package cn.powertime.evaluation.web.controller;

import cn.powertime.evaluation.entity.CpsAutomatic;
import cn.powertime.evaluation.entity.CpsExaminationResult;
import cn.powertime.evaluation.facade.CpsTestPaperFacade;
import cn.powertime.evaluation.vo.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 试卷表 前端控制器
 * </p>
 *
 * @author Yang Xin
 * @since 2019-09-25
 */
@RestController
@RequestMapping("/web/testPaper")
@Api(value = "/web/testPaper", tags = "试卷接口", produces = MediaType.ALL_VALUE)
@Validated
public class CpsTestPaperController extends BaseController {

    @Autowired
    private CpsTestPaperFacade cpsTestPaperFacade;

    @ApiOperation(value = "成为测评师-课后作业/课程刷题-试卷列表接口")
    @PostMapping(value = "/courseWorkTestPaperList")
    public Object courseWorkTestPaperList(@Validated @RequestBody ParamPageVo<ReqCourseTestPaperSearchVo> vo) {
        Page<RespTestPaperPageVo> list = this.cpsTestPaperFacade.courseWorkTestPaperList(vo);
        return success(list);
    }

    @ApiOperation(value = "成为测评师-课后作业-习题列表接口")
    @GetMapping(value = "/courseWorkTopicList")
    @ApiImplicitParam(name = "sectionId", value = "小节id", required = true, dataType = "Long")
    public Object courseWorkTopicList(@NotNull(message = "小节id不能为空") @RequestParam Long sectionId) {
        RespTestPaperVo testPaperVo = this.cpsTestPaperFacade.courseWorkTopicList(sectionId);
        if(testPaperVo == null) {
            return success(ImmutableMap.of());
        }
        return success(testPaperVo);
    }

    @ApiOperation(value = "成为测评师-考试练习-课程刷题-习题列表接口")
    @GetMapping(value = "/courseTopicList")
    @ApiImplicitParam(name = "courseId", value = "课程id", required = true, dataType = "Long")
    public Object courseTopicList(@NotNull(message = "课程id不能为空") @RequestParam Long courseId) {
        RespTestPaperVo testPaperVo = this.cpsTestPaperFacade.courseTopicList(courseId);
        if(testPaperVo == null) {
            return success(ImmutableMap.of());
        }
        return success(testPaperVo);
    }

    @ApiOperation(value = "成为测评师-考试练习-智能练习-习题列表接口")
    @GetMapping(value = "/automaticTopicList")
    public Object automaticTopicList() {
        RespAutomaticVo vo = cpsTestPaperFacade.automaticTopicList();
        return success(vo);
    }

    @ApiOperation(value = "保存智能练习试卷接口")
    @PostMapping(value = "/saveAutomaticTestPaper")
    public Object saveAutomaticTestPaper(@Validated @RequestBody ReqAutomaticTestPaperVo vo) {
        this.cpsTestPaperFacade.saveAutomaticTestPaper(vo);
        return success("保存成功");
    }

    @ApiOperation(value = "成为测评师-考试练习-真题模拟-试卷列表接口")
    @PostMapping(value = "/simulateExaminationPaperList")
    public Object simulateExaminationPaperList(@Validated @RequestBody PageVo vo) {
        Page<RespTestPaperPageVo> list = this.cpsTestPaperFacade.simulateExaminationPaperList(vo);
        return success(list);
    }

    @ApiOperation(value = "成为测评师/测评师训练场-通过试卷id查询习题列表接口")
    @GetMapping(value = "/testPaperTopicList")
    @ApiImplicitParam(name = "testPaperId", value = "试卷id", required = true, dataType = "Long")
    public Object testPaperTopicList(@NotNull(message = "试卷id不能为空") @RequestParam Long testPaperId) {
        RespTestPaperVo testPaperVo = this.cpsTestPaperFacade.testPaperTopicList(testPaperId);
        if(testPaperVo == null) {
            return success(ImmutableMap.of());
        }
        return success(testPaperVo);
    }

    @ApiOperation(value = "测评师训练场-测评练习-试卷列表接口")
    @PostMapping(value = "/testing")
    public Object testing(@Validated @RequestBody ParamPageVo<ReqTestPaperSearchVo> vo) {
        Page<RespTestPaperPageVo> list = this.cpsTestPaperFacade.testing(vo);
        return success(list);
    }

    @ApiOperation(value = "保存试卷接口")
    @PostMapping(value = "/saveTestPaper")
    public Object saveTestPaper(@Validated @RequestBody ReqTestPaperVo vo) {
        this.cpsTestPaperFacade.saveTestPaper(vo);
        return success("保存成功");
    }

    @ApiOperation(value = "提交试卷接口")
    @PostMapping(value = "/submitTestPaper")
    public Object submitTestPaper(@Validated @RequestBody ReqTestPaperVo vo) {
        CpsExaminationResult result = this.cpsTestPaperFacade.submitTestPaper(vo);
        return success(result);
    }

    @ApiOperation(value = "提交智能练习试卷接口")
    @PostMapping(value = "/submitAutomaticTestPaper")
    public Object submitAutomaticTestPaper(@Validated @RequestBody ReqAutomaticTestPaperVo vo) {
        CpsAutomatic result = this.cpsTestPaperFacade.submitAutomaticTestPaper(vo);
        return success(result);
    }

}
